import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
// import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:vehicle_management/pages/my_task_page/traisitEnd.dart';
import 'package:vehicle_management/pages/my_task_page/tranisitIng.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:vehicle_management/config/Config.dart';
class TaskPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new TaskPageState();
  }
}

class TaskPageState extends State<TaskPage>
    with SingleTickerProviderStateMixin {
  TabController _tabController;
  bool toggle = true;

  ///运输结束数据
  List _trainsitEndlist = [];
  ///运输结束数据
  List _trainsitInglist = [];
  int _page = 1;
  ScrollController _scrollControllerTrainsitEnd = new ScrollController(); //上拉加载
  ScrollController _scrollControllerTrainsitIng = new ScrollController(); //上拉加载
  bool hasMore = true;
  
  /// 运输中数据
  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }

  void initState() {
    super.initState();
    _tabController = new TabController(vsync: this, length: 2);
    this._getDataTransitEnd();
    this._getDataTransitIng();

    /// 监听滚动事件
    _scrollControllerTrainsitEnd.addListener(() {
      if (_scrollControllerTrainsitEnd.position.pixels >
          _scrollControllerTrainsitEnd.position.maxScrollExtent - 40) {
        this._getDataTransitEnd();
      }
    });
    _scrollControllerTrainsitIng.addListener(() {
      if (_scrollControllerTrainsitIng.position.pixels >
          _scrollControllerTrainsitIng.position.maxScrollExtent - 40) {
        this._getDataTransitIng();
      }
    });
  }

  ///获取运输结束的列表
  _getDataTransitEnd() async {
    if (this.hasMore) {
      var apiUrl = "${Config.transitUrl}?page=$_page&limit=10&taskStatus=1&appKey=M8oZL5Fw+C9ASHe43cfLH/jgQq+BOsPgH/HQOT9JOOY=";
      print(apiUrl);
      var response = await Dio().get(apiUrl);
      print((response.data)['data']);
      var res = (response.data)['data'];
      setState(() {
        this._trainsitEndlist.addAll(res);

        ///防止下一页数据覆盖上一页数据
        this._page++;
      });

      ///判断是否是最后一页
      if (res.length < 20) {
        setState(() {
          this.hasMore = false;
        });
      }
    }
  }

  _getDataTransitIng() async {
    if (this.hasMore) {
       var apiUrl = "${Config.transitUrl}?page=$_page&limit=10&taskStatus=1&appKey=M8oZL5Fw+C9ASHe43cfLH/jgQq+BOsPgH/HQOT9JOOY=";
      var response = await Dio().get(apiUrl);
      print((response.data)['data']);
      var res = (response.data)['data'];
      setState(() {
        this._trainsitInglist.addAll(res);

        ///防止下一页数据覆盖上一页数据
        this._page++;
      });

      ///判断是否是最后一页
      if (res.length < 20) {
        setState(() {
          this.hasMore = false;
        });
      }
    }
  }

//设置运输中数据
  Widget _transittingListData(context, index) {
    return _transitingListItem(context, index);
  }

  //运输中列表的item
  Widget _transitingListItem(context, index) {
    return tranisitIng(this._trainsitInglist, index, this.hasMore,context);
  }

  // 运输结束列表
  Widget _transitEndListData(context, index) {
    return _transitEndListItem(context, index);
  }

  //运输结束列表的item
  _transitEndListItem(context, index) {
    return traisitEnd(this._trainsitEndlist, index, this.hasMore,context);
  }

  ///运输结束的下拉刷新
  Future<void> _onRefreshTransitEnd() async {
    print("下拉刷新");
    await Future.delayed(Duration(milliseconds: 2000), () {
      _getDataTransitEnd();
    });
  }

  ///运输中的下拉刷新
  Future<void> _onRefreshTransitIng() async {
    print("下拉刷新");
    await Future.delayed(Duration(milliseconds: 2000), () {
      _getDataTransitIng();
    });
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)..init(context);
    return Scaffold(
      appBar: AppBar(
        title: Text('我的任务'),
        bottom: PreferredSize(
          preferredSize: Size.fromHeight(40),
          child: Material(
            color: Colors.white,
            child: TabBar(
                controller: _tabController,
                indicatorSize: TabBarIndicatorSize.label,
                indicatorColor: Color(0xffFB5858),
                unselectedLabelColor: Color(0xff696969),
                labelColor: Color(0xffFB5858),
                labelStyle: TextStyle(
                  fontSize:  ScreenUtil().setSp(30)
                ) ,
                tabs: <Widget>[
                  Tab(
                    // icon:  Icon(Icons.directions_bike),
                    text: '运输中',
                  ),
                  Tab(text: '运输结束'),
                ]),
          ),
        ),
      ),
      body: TabBarView(
        controller: _tabController,
        children: <Widget>[
          RefreshIndicator(
            child: ListView.builder(
              controller: _scrollControllerTrainsitIng,
              itemCount: this._trainsitInglist.length,
              itemBuilder: this._transittingListData,
            ),
            onRefresh: _onRefreshTransitIng,
          ),

          ///运输结束
          RefreshIndicator(
            child: ListView.builder(
              controller: _scrollControllerTrainsitEnd,
              itemCount: this._trainsitEndlist.length,
              itemBuilder: this._transitEndListData,
            ),
            onRefresh: _onRefreshTransitEnd,
          ),
        ],
      ),
    );
  }
}
